package com.obelisco.vista.herramientas;

import java.lang.reflect.InvocationTargetException;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.List;

import nextapp.echo2.app.SelectField;
import nextapp.echo2.app.TextArea;
import nextapp.echo2.app.TextField;
import nextapp.echo2.app.list.ListModel;

import org.apache.commons.beanutils.PropertyUtils;

import com.obelisco.modelo.data.DataGenerica;
import com.obelisco.vista.controles.RadioButton;

import com.jgoodies.binding.adapter.echo.ComboBoxAdapter;
import com.jgoodies.binding.adapter.echo.RadioButtonAdapter;
import com.jgoodies.binding.adapter.echo.TextComponentConnector;
import com.jgoodies.binding.beans.PropertyAdapter;
import com.jgoodies.binding.list.echo.ArrayListModel;
import com.jgoodies.binding.list.echo.SelectionInList;
import com.jgoodies.binding.value.ConverterFactory;
import com.jgoodies.binding.value.ValueModel;

public class BindingHelper {
	
public static void setTxtBinding(DataGenerica dataGenerica, String propiedad, TextField txtField){

	try {			
			Class clase = PropertyUtils.getPropertyType(dataGenerica, propiedad);			
			if("java.lang.String".equals(clase.getName())){				
				ValueModel model = new PropertyAdapter(dataGenerica, propiedad,true);
				TextComponentConnector connector =  new TextComponentConnector(model, txtField);
				connector.updateTextComponent();
			}
			else if("java.lang.Long".equals(clase.getName())){				
				if(PropertyUtils.getProperty(dataGenerica, propiedad)!=null){
					Long auxLong = (Long)PropertyUtils.getProperty(dataGenerica, propiedad);
					dataGenerica.setAuxBinding(auxLong.toString());
					ValueModel model = new PropertyAdapter(dataGenerica, "auxBinding",true);
					TextComponentConnector connector =  new TextComponentConnector(model, txtField);
					connector.updateTextComponent();					
				}
				ValueModel model = new PropertyAdapter(dataGenerica, propiedad,true);
				ValueModel model2 = ConverterFactory.createStringConverter(model, NumberFormat.getIntegerInstance());
				TextComponentConnector connector =  new TextComponentConnector(model2, txtField);
			}
			else if("java.lang.Double".equals(clase.getName())){				
				if(PropertyUtils.getProperty(dataGenerica, propiedad)!=null){
					Double auxLong = (Double)PropertyUtils.getProperty(dataGenerica, propiedad);
					dataGenerica.setAuxBinding(auxLong.toString());
					ValueModel model = new PropertyAdapter(dataGenerica, "auxBinding",true);
					TextComponentConnector connector =  new TextComponentConnector(model, txtField);
					connector.updateTextComponent();					
				}
				ValueModel model = new PropertyAdapter(dataGenerica, propiedad,true);
				ValueModel model2 = ConverterFactory.createStringConverter(model, NumberFormat.getNumberInstance());
				TextComponentConnector connector =  new TextComponentConnector(model2, txtField);
			}
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
	public static void setTxtBinding(DataGenerica dataGenerica, String propiedad, TextArea txtArea){
		ValueModel model = new PropertyAdapter(dataGenerica, propiedad, true);
		TextComponentConnector connector =  new TextComponentConnector(model, txtArea);
		connector.updateTextComponent();
	}
	
	public static void setRdbBinding(DataGenerica dataGenerica, String propiedad, RadioButton rdb, Object valor){
		ValueModel model = new PropertyAdapter(dataGenerica, propiedad, true);
		RadioButtonAdapter connector =  new RadioButtonAdapter(model, valor);
		rdb.setModel(connector);
		//connector.updateTextComponent();
	}
	
	public static void setCmbBinding(DataGenerica objeto, String propiedadMostrar, List listaObjetos, SelectField selectField){
		try{
			//ValueModel model = new PropertyAdapter(objeto, propiedadMostrar, true);
/*			ValueModel model = new ValueHolder(objeto);
			ComboBoxAdapter adapter = new ComboBoxAdapter(listaObjetos, model);
			selectField.setModel(adapter);
			if(objeto!=null){
				selectField.setSelectedIndex(listaObjetos.indexOf(PropertyUtils.getProperty(objeto, objeto.getPropiedadComboBinding())));				
			}			
*/
			 ListModel countryListModel = new ArrayListModel(Arrays.asList(listaObjetos));
			 ValueModel model = new PropertyAdapter(objeto, propiedadMostrar, true);
			 SelectionInList sil     = new SelectionInList(listaObjetos, model);
			 ComboBoxAdapter adapter = new ComboBoxAdapter(sil);
			 selectField.setModel(adapter);
		 

		}
		catch(Exception e){			
			e.printStackTrace();
		}		
	}

}
